package cn.iocoder.yudao.module.dine.desk.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.ShopSimpleVO;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.dine.desk.convert.DeskCategoryConvert;
import cn.iocoder.yudao.module.dine.desk.dao.DeskCategoryMapper;
import cn.iocoder.yudao.module.dine.desk.pojo.dto.DeskCategoryQueryDTO;
import cn.iocoder.yudao.module.dine.desk.pojo.dto.DeskCategorySaveDTO;
import cn.iocoder.yudao.module.dine.desk.pojo.po.DeskCategory;
import cn.iocoder.yudao.module.dine.desk.pojo.vo.DeskCategoryVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务接口类
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/11
 */
@Service
public class DeskCategoryService extends BaseService<DeskCategory, DeskCategoryVO, DeskCategoryQueryDTO> implements CrudService<DeskCategory, DeskCategoryVO, DeskCategorySaveDTO, DeskCategoryQueryDTO> {
    @Resource
    private DeskCategoryMapper deskCategoryMapper;

    @Resource
    private ShopInfoApi shopInfoApi;

    @Override
    public DeskCategoryMapper getInjectedMapper() {
        return deskCategoryMapper;
    }

    @Override
    public DeskCategory convertToEntity(DeskCategorySaveDTO param) {
        return DeskCategoryConvert.INSTANCE.toEntity(param);
    }

    @Override
    public void beforeCreate(DeskCategory entity, @Nullable Long userId) {
        entity.setShopId(getLoginUserLastShopId());
    }

    @Override
    public void beforeUpdate(DeskCategory entity) {
        Assertions.owner(entity.getShopId(), getLoginUserLastShopId());
    }

    @Override
    public void beforeDelete(DeskCategory entity) {
        Assertions.owner(entity.getShopId(), getLoginUserLastShopId());
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<DeskCategory> buildQueryWrapper(DeskCategoryQueryDTO param) {
        CoreQueryWrapper<DeskCategory> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("parent_id", param.getParentId());
        wrapper.likeIfPresent("name", param.getName());
        return wrapper;
    }

    @Override
    public DeskCategoryVO convertToViewObject(DeskCategory entity) {
        return DeskCategoryConvert.INSTANCE.toViewObject(entity);
    }

    @Override
    public List<DeskCategoryVO> convertToViewObject(List<DeskCategory> entities) {
        Set<Long> shopIds = convertSet(entities, DeskCategory::getShopId);
        Map<Long, ShopSimpleVO> shops = shopInfoApi.getShopSimpleMap(shopIds);
        List<DeskCategoryVO> list = super.convertToViewObject(entities);
        for (DeskCategoryVO vo : list) {
            vo.setShop(shops.getOrDefault(vo.getShopId(), ShopSimpleVO.EMPTY));
        }
        return list;
    }

    @DataPermission(enable = false)
    public List<DeskCategoryVO> getEnabledAll(Long shopId) {
        List<DeskCategory> list = getInjectedMapper().selectEnabledAll(shopId);
        return convertToViewObject(list);
    }

}
